package ei

import (
	"bufio"
	"fmt"
	"os"
	"os/exec"
	"strings"
)

func main() {
	i := CreateInterpreter()
	i.RunSimple("a=1")
}

func SimpleCycle(interpreter Interpreter) {
	for true {
		var multiLine bool
		var inputs string
		inputReader := bufio.NewReader(os.Stdin)
		for {
			if multiLine {
				fmt.Print("... ")
			} else {
				fmt.Print(">>> ")
			}
			inputString, err := inputReader.ReadString('\n')
			// println(inputString)
			if err != nil {
				fmt.Println("读取输入内容失败", err)
			}
			inputs += inputString
			inputTest := strings.ReplaceAll(strings.ReplaceAll(strings.Trim(inputString, " "), "\r", ""), "\n", "")
			if inputTest == "cls" { // 清屏
				c := exec.Command("cmd", "/c", "cls")
				c.Stdout = os.Stdout
				c.Run()
				inputs = ""
				multiLine = false
				continue
			}
			if inputTest == "" { // 空行
				if multiLine {
					runTool_FinishInput(inputs, interpreter)
					inputs = ""
					multiLine = false
				}
			} else if inputTest[len(inputTest)-1:] == ":" {
				multiLine = true
			}
			if !multiLine {
				runTool_FinishInput(inputs, interpreter)
				inputs = ""
			}
		}
	}
}

func runTool_FinishInput(in string, inter Interpreter) {
	inter.RunSimple(in)
}

func checkErr(err error) bool {
	if err != nil {
		panic(err)
	} else {
		return true
	}
}
